﻿using System;
using System.Reflection;

namespace FluentResourceMapping
{
    public class MemberResourceMapping
    {
        private Type resourceType;
        private string resourceKey;
        private MemberInfo member;

        public MemberResourceMapping(MemberInfo member, Type resourceType, string resourceKey)
        {
            this.resourceType = resourceType;
            this.resourceKey = resourceKey;
            this.member = member;
        }

        public Type ResourceType
        {
            get { return this.resourceType; }
        }

        public string ResourceKey
        {
            get { return this.resourceKey; }
        }

        public MemberInfo Member
        {
            get { return this.member; }
        }

        public string Value // string ?
        {
            get
            {
                PropertyInfo propertyInfo = ResourceType.GetProperty(ResourceKey,
                                                                     BindingFlags.Static | BindingFlags.Public);
                if (propertyInfo == null)
                    throw new ApplicationException(
                        string.Format(
                            "The property {0} is either not accessible for resouce type {1} or is not static.",
                            ResourceKey, ResourceType.FullName));

                return (string) propertyInfo.GetValue(propertyInfo, null);
            }
        }
    }
}